package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.serve.domain.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private final String CACHE_KEY = "nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int flag = nursingProjectMapper.insertNursingProject(nursingProject);
        deleteCache();
        return flag;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        //保存数据
        int flag = nursingProjectMapper.updateNursingProject(nursingProject);

        deleteCache();
        return flag;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        //保存数据
        int flag = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        //删除缓存
        deleteCache();
        return flag;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        //保存数据
        int flag = nursingProjectMapper.deleteNursingProjectById(id);
        deleteCache();
        return flag;
    }

    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY);
    }

    /**
     * 获取护理项目列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllNursingProjectList() {
        //从缓存中查数据
        List<NursingProjectVo> nursingProjectVos = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY);
        //判断缓存是否有数据
        if (nursingProjectVos != null && nursingProjectVos.size() > 0) {
            return nursingProjectVos;
        }
        //查询数据库(说明缓存中没有数据)
        nursingProjectVos = nursingProjectMapper.getAllNursingProjectList();
        //保存到缓存中
        redisTemplate.opsForValue().set(CACHE_KEY, nursingProjectVos);
        return nursingProjectVos;

    }

    /**
     * 分页查询项目列表
     *
     * @param status
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<NursingProject> pageNameAndStatus(Integer status, String name, Integer pageNum, Integer pageSize) {
        Page<NursingProject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, NursingProject::getName, name)
                .eq(status != null, NursingProject::getStatus, status);
         page = page(page, queryWrapper);
       return page.getRecords();
    }
}
